package L618;

/**
 * @program: javase
 * @description: 最长公共子序列
 * @author: luolidong
 * @create: 2021-06-18 16:45
 * @version: 1.0
 */
public class LongestCommonSubsequence1143 {
    public int longestCommonSubsequence(String text1, String text2) {
//        预留第0行第0列，从1开始记录
        int[][] dp = new int[text1.length() + 1][text2.length() + 1];
        for (int i = 1; i <= text1.length(); i++) {
//            从0开始遍历字符串
            char c = text1.charAt(i - 1);
            for (int j = 1; j <= text2.length(); j++) {
                char c1 = text2.charAt(j - 1);
                if (c == c1) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[text1.length()][text2.length()];
    }

    public static void main(String[] args) {
        int i = new LongestCommonSubsequence1143()
                .longestCommonSubsequence("abdcde", "acdefde");
        System.out.println(i);
    }
}
